# pylint: disable-msg=E1101
"""
@author: Sergiy Tsymbalyuk
"""

from datetime import datetime, timedelta

from pylons import request

from chowa.lib.base import render, Session
from chowa.lib.site.layout import PanelComponent
from chowa.model.auth import User
from chowa.model.abstract import Person


class OnlineUsers(object):
    __metaclass__ = PanelComponent
    
    def __init__(self, options={}):
        pass
    
    def render(self):
        identity = request.environ.get('repoze.who.identity')
        me = identity.get('user') if identity else None
        critical_time = datetime.now() - timedelta(minutes=30)
        query = Session.query(User).filter(User.last_activity > critical_time)
        query = query.outerjoin(Person)
        users = query.order_by(Person.surname,
                               Person.name,
                               Person.patronymic).all()
        return render('/social/messaging/online_users.html',
                      extra_vars={'users': users, 'me': me})
    